<?php

$types=array(
	'.JPG',
	'.jpg',
	'.jpeg',
	'.png',
	'.gif',
	'.bmp',
	'.x-png');
$maxfilesize = 2000000;
$thumbwidth	=100;
$thumbheight=75;
$foldera="user_upfile/pic/";
$thumb = 'thumb/';
$uptimeall=date("Y/m/d/",$_SERVER['REQUEST_TIME']);
$folder=$foldera.$uptimeall; //上传文件路径

//普通上传
function upimage($upfile,$imgthumb=1){
$maxfilesize = 2000000;
$thumbwidth	=100;
$thumbheight=75;
$foldera="user_upfile/pic/";
$thumb = 'thumb/';
$uptimeall=date("Y/m/d/",$_SERVER['REQUEST_TIME']);
$folder=$foldera.$uptimeall; //上传文件路径
	if($imgthumb!=="1"){
		$imgthumb=$imgthumb;	//是否生成缩略图(1为生成,其他为不生成);
	}
	$uptypes=array(
		'image/jpg',
		'image/jpeg',
		'image/png',
		'image/pjpeg',
		'image/gif',
		'image/bmp',
		'image/x-png');
	if (!is_uploaded_file($_FILES["$upfile"]["tmp_name"])){
		echo "图片不存在!";
		exit;
	}
	$file = $_FILES["$upfile"];
	if($maxfilesize < $file["size"]){
		echo "文件太大!".$maxfilesize;
		exit;
	}
	if(!in_array($file["type"], $uptypes)){
		echo "文件类型不符!".$file["type"];
		exit;
	}
	if(!file_exists($folder)){
		mkdirm($folder);
	}
	if(!file_exists($folder.$thumb)){
		mkdirm($folder.$thumb);
	}

	$filename=$file["tmp_name"];
	$image_size = getimagesize($filename);
	$pinfo=pathinfo($file["name"]);
	$ftype=$pinfo['extension'];
	$savepath = $folder.time().".".$ftype;
	if (file_exists($savepath) && $overwrite != true){
		echo "同名文件已经存在了";
		exit;
	}
	if(!move_uploaded_file ($filename, $savepath)){
		echo "移动文件出错";
		exit;
	}
	if($imgthumb==1){
		$pinfo=pathinfo($savepath);
		$savethumbpath=$folder.$thumb.$pinfo["basename"];
		thumb($savepath,$savethumbpath,$thumbwidth,$thumbheight);
	}
	$upimage[0] = " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$folder.$fname."</font><br>";
	$upimage[1] = " 宽度:".$image_size[0]." 长度:".$image_size[1]."<br> 大小:".$file["size"]." bytes";
	$upimage[2] = "<br>图片预览:<br><img src='$folder$thumb$fname' width='($thumbwidth)' height='($thumbheight)'>";
	$upimage[3] = $uptimeall.$fname;
	$upimage[4] = $uptimeall.$thumb.$fname;
	return $upimage;
}
//swf上传
if($_GET['thumb']){
mb_http_input("utf-8");
mb_http_output("utf-8");
$type=filekzm($_FILES["Filedata"]["name"]);
if (!in_array($type, $types)){
	echo "上传失败，请检查文件类型是否符合标准<br />文件类型：".$type;
	exit;
}else if(in_array($type, $types)){
	if ($_FILES["Filedata"]["size"] > $maxfilesize){
		echo "上传失败，请检查文件大小是否符合标准<br />文件大小:".($_FILES["Filedata"]["size"] / 1024) . " Kb";
		exit;
	}
	if ($_FILES["Filedata"]["error"] > 0){
		echo "返回错误: " . $_FILES["Filedata"]["error"] . "<br />";
	}else{
		if(!file_exists($folder)){
			mkdirm($folder);
		}
		if(!file_exists($folder.$thumb)){
			mkdirm($folder.$thumb);
		}
		$img_name = $_FILES["Filedata"]["name"];
		$img_length = $_FILES["Filedata"]["size"];
		$img_size = getimagesize($_FILES["Filedata"]["tmp_name"]);
		$savepath = $folder.time().$type;
		if (file_exists($savepath)){
			echo "文件已经存在.";
		}else{
			move_uploaded_file($_FILES["Filedata"]["tmp_name"],
			$savepath);
		}
		//生成缩略图
		if($_GET['thumb']=='1'){
			$pinfo=pathinfo($savepath);
			$savethumbpath=$folder.$thumb.$pinfo["basename"];
			thumb($savepath,$savethumbpath,$thumbwidth,$thumbheight);
		}
		
			echo "储存至: ".$savepath;
			echo "<img src='".$savethumbpath."'>";
	}
}}
function filekzm($a){
	$c=strrchr($a,'.');
	if($c){
		return $c;
	}else{
		return '';
	}
}
function mkdirm($path){ 
	if(!file_exists($path)){
		mkdirm(dirname($path));
		mkdir($path, 0777);
	}
}
function thumb($savepath,$savethumbpath,$thumbwidth,$thumbheight){
			list($width,$height) = getimagesize($savepath);
			$thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
			if($width>$thumbwidth || $height>$thumbheight){
				$matchimg= $thumbwidth*$height/$thumbheight;
				if($width>$matchimg){
					$newheight=($thumbwidth*$height)/$width;
					$newwidth=$thumbwidth;
					$fheight=($thumbheight-$newheight)/2;
					$fwidth=0;
				}else if($width<$matchimg){
					if($thumbwidth<$thumbheight){
						$newwidth=($thumbheight*$width)/$thumbwidth;
						$newheight=$thumbheight;
						$fwidth=($thumbwidth-$newwidth)/2;
						$fheight=0;
					}else{
						$newwidth=$width*$thumbheight/$height;
						$newheight=$thumbheight;
						$fwidth=($thumbwidth-$newwidth)/2;
						$fheight=0;
					}
				}else if($width==$matchimg){
					$newwidth=$thumbwidth;
					$newheight=$thumbheight;
					$fwidth=0;
					$fheight=0;
				}
				$source = imagecreatefromjpeg($savepath);
				imagecopyresized($thumb, $source, $fwidth, $fheight, $fwidth, $fheight, $newwidth, $newheight, $width, $height);
				imagejpeg($thumb,$savethumbpath);
			}else{
				copy($savepath,$savethumbpath);
			}
}
//	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//		$upfile = 'upfile';
//		$upimage = upimage($upfile);
//	}

//随机函数
function randpw($length)
{
$output='';
for ($a = 0; $a < $length; $a++) {
$output .= chr(mt_rand(33, 126));    //生成php随机数
}
return $output;
}
//删除
function Delete_file($path){
	if(file_exists($path)){
		unlink($path);
	}
}
?>
